<template>
  <!-- 审核规则 -- 工作流 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/checkRule']"
        ></el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="selection.length == 0"
          @click="batch_del"
          v-auth-button="['deleteList/checkRule']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="名称 / 类型"
            clearable
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        @selection-change="selectionChange"
        stripe
        remoteSort
        remoteFilter
        @filterType="filterType"
      >
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column
          label="ID"
          prop="id"
          width="80"
          sortable
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="名称" prop="name" width="250">
        </el-table-column>
        <el-table-column label="类型" prop="typeName" width="200">
          <!-- <template #default="scope">
            <p>{{ filterType(scope.row.type) }}</p>
          </template> -->
        </el-table-column>
        <el-table-column
          label="是否默认"
          prop="defaultOrNot"
          width="200"
          sortable
        >
          <!-- 是否默认：0否，1是 -->
          <template #default="scope">
            <el-switch
              v-model="scope.row.defaultOrNot"
              :inactive-value="0"
              :active-value="1"
              @change="defaultOrNotChange($event, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state" width="200" sortable>
          <!-- 0禁用,1开启 -->
          <template #default="scope">
            <el-switch
              v-model="scope.row.state"
              :inactive-value="0"
              :active-value="1"
              @change="stateChange($event, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="是否免审" prop="state" width="200" sortable>
          <!-- 0,不免，1免审-->
          <template #default="scope">
            <el-switch
              v-model="scope.row.exempt"
              :inactive-value="0"
              :active-value="1"
              @change="exemptChange($event, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          align="right"
          min-width="160"
        >
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/checkRule']"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/checkRule']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
export default {
  name: 'workflowIndex',
  data () {
    return {
      apiObj: this.$API.workflow.getWorkflowList,
      userList: [],
      selection: [],
      searchValue: {
        search: null
      },
      workflowType: []//工作流类型
    }
  },
  beforeMount () {

  },
  methods: {
    //添加
    add () {
      this.$router.push({
        path: '/auth/checkRule/addForm',
        query: {
          mode: 'add'
        }
      })
    },
    //编辑
    table_edit (row) {
      this.$router.push({
        path: '/auth/checkRule/addForm',
        query: {
          mode: 'edit'
        }
      })
      localStorage.setItem('workflowItem', JSON.stringify(row))
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: '/auth/checkRule/addForm',
        query: {
          mode: 'show'
        }
      })
      localStorage.setItem('workflowItem', JSON.stringify(row))
    },
    //删除
    async table_del (row) {
      var res = await this.$API.workflow.deleteWorkflow.post({ ids: row.id })
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      }
    },
    //批量删除
    batch_del () {
      this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
        type: 'warning'
      }).then(async () => {
        const loading = this.$loading()
        let ids = []
        this.selection.forEach(item => {
          ids.push(item.id)
        })
        console.log(ids)
        let res = await this.$API.workflow.deleteWorkflow.post({ ids: ids.join(',') })
        console.log(res)
        this.$refs.table.refresh()
        loading.close()
        this.$message.success("操作成功")
      }).catch(() => {

      })
    },
    //表格选择后回调事件
    selectionChange (selection) {
      this.selection = selection
    },
    //修改状态
    async stateChange (val, row) {
      let data = {
        id: row.id,
        state: row.state
      }
      let res = await this.$API.workflow.updateWorkflow.post(data)
      this.$refs.table.refresh()
      if (res.code == 200) {
        this.$message.success("操作成功")
      }
    },
    // 是否是否默认
    async defaultOrNotChange (val, row) {
      let data = {
        id: row.id,
        defaultOrNot: row.defaultOrNot
      }

      let res = await this.$API.workflow.updateWorkflow.post(data)
      this.$refs.table.refresh()
      if (res.code == 200) {
        this.$message.success("操作成功")
      }
    },

    // 是否免审
    async exemptChange (val, row) {
      let data = {
        id: row.id,
        exempt: row.exempt
      }
      let res = await this.$API.workflow.updateWorkflow.post(data)
      this.$refs.table.refresh()
      if (res.code == 200) {
        this.$message.success("操作成功")
      }
    },
    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSuccess (data, mode) {
      if (mode == 'add') {
        // data.id = new Date().getTime()
        // this.$refs.table.tableData.push(data)
        this.$refs.table.getData()
      } else if (mode == 'edit') {
        this.$refs.table.tableData.filter(item => item.id === data.id).forEach(item => {
          Object.assign(item, data)
        })
      }
    },
    // 处理工作流类型
    filterType (data) {
      let tableData = data
      queryTypeByDic('streamCode').then(res => {
        let list = res
        tableData.forEach(item => {
          list.forEach(t_item => {
            if (item.type == t_item.value) {
              item.typeName = t_item.name
            }
          })
        })
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.el-table {
  :deep(.el-table__header) {
    width: 100% !important;
  }
  :deep(.el-table__body) {
    width: 100% !important;
  }
}
</style>
